import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		isLogin: false,
		isSDKReady: false, // TIM SDK 是否 ready
		conversationActive: {}, //聊天进行中的会话
		toUserId: '', //聊天对象id
		conversationList: [], //会话列表
		currentMessageList: [], //消息列表
		unread: 0, // 会话未读总数

		userSig: '', // 登录密码
		loadingShow: false, // 全局加载Loading的开关变量
		keyHeight: 0, // 软键盘的高度,
		noKeyHeight: true, // 控制是否计算键盘高度的变量,
		rpxSize: 0, // 设备宽度/750 得到 1rpx 的相对px

		showBindWx: true,

	},
	mutations: {
		// 更新相对px
		changeRpxSize(state, value) {
			state.rpxSize = value;
		},
		// 更新控制是否计算键盘高度的变量
		changeNoKeyHeight(state, value) {
			state.noKeyHeight = value;
		},
		// 更新软键盘的高度
		changeKeyHeight(state, value) {
			state.keyHeight = value;
		},
		// 更新Loading开关
		changeLoadingShow(state, value) {
			state.loadingShow = value;
		},
		//更新登录状态
		toggleIsLogin(state, isLogin) {
			state.isLogin = typeof isLogin === 'undefined' ? !state.isLogin : isLogin
		},
		//更新TIMSDK状态
		toggleIsSDKReady(state, isSDKReady) {
			state.isSDKReady = typeof isSDKReady === 'undefined' ? !state.isSDKReady : isSDKReady
		},

		//更新绑定微信号状态
		changeBindWx(state, value) {
			state.showBindWx = value;
		},
		//退出登录重置状态
		reset(state) {
			state.isLogin = false
			state.isSDKReady = false
			state.conversationList = []
			state.unread = 0
		},
		//选择好友聊天--创建会话/拼接会话id
		createConversationActive(state, toUserId) {
			state.conversationActive.conversationID = 'C2C' + toUserId
			state.toUserId = toUserId
			state.currentMessageList = []
		},
		//选择已有会话聊天--更新选中会话详情
		updateConversationActive(state, conversationItem) {
			state.conversationActive = Object.assign({}, conversationItem);
			state.toUserId = conversationItem.userProfile.userID
			state.currentMessageList = []
		},
		//会话未读总数
		onTotalUnreadMessageCountUpdated(state, unread) {
			state.unread = unread
		},
		//更新会话列表
		updateConversationList(state, newConversationList) {
			state.conversationList = [{
				"conversationID": "C2C10087",
				"unreadCount": 0,
				"type": "C2C",
				"lastMessage": {
					"messageForShow": "暂无新信息"
				},
				"userProfile": {
					"userID": "10087",
					"nick": "点赞",
					"gender": "",
					"birthday": 0,
					"location": "",
					"selfSignature": "",
					"allowType": "AllowType_Type_AllowAny",
					"language": 0,
					"avatar": "",
					"messageSettings": 0,
					"adminForbidType": "AdminForbid_Type_None",
					"level": 0,
					"role": 0,
					"lastUpdatedTime": 0,
					"profileCustomField": []
				},
				"actionShow": false
			}, {
				"conversationID": "C2C10088",
				"unreadCount": 0,
				"type": "C2C",
				"lastMessage": {
					"messageForShow": "暂无新信息"
				},
				"_isInfoCompleted": false,
				"peerReadTime": 0,
				"groupAtInfoList": [],
				"remark": "",
				"isPinned": true,
				"messageRemindType": "",
				"markList": [],
				"customData": "",
				"conversationGroupList": [],
				"userProfile": {
					"userID": "10088",
					"nick": "评论",
					"gender": "",
					"birthday": 0,
					"location": "",
					"selfSignature": "",
					"allowType": "AllowType_Type_AllowAny",
					"language": 0,
					"avatar": "",
					"messageSettings": 0,
					"adminForbidType": "AdminForbid_Type_None",
					"level": 0,
					"role": 0,
					"lastUpdatedTime": 0,
					"profileCustomField": []
				},
				"actionShow": false
			}, {
				"conversationID": "C2C10089",
				"unreadCount": 0,
				"type": "C2C",
				"lastMessage": {
					"messageForShow": "暂无新信息"
				},
				"_isInfoCompleted": false,
				"peerReadTime": 0,
				"groupAtInfoList": [],
				"remark": "",
				"isPinned": true,
				"messageRemindType": "",
				"markList": [],
				"customData": "",
				"conversationGroupList": [],
				"userProfile": {
					"userID": "10089",
					"nick": "小碰碰(系统通知)",
					"gender": "",
					"birthday": 0,
					"location": "",
					"selfSignature": "",
					"allowType": "AllowType_Type_AllowAny",
					"language": 0,
					"avatar": "https://pengtou-1322587508.cos.ap-shanghai.myqcloud.com/static/images/community/system.png",
					"messageSettings": 0,
					"adminForbidType": "AdminForbid_Type_None",
					"level": 0,
					"role": 0,
					"lastUpdatedTime": 0,
					"profileCustomField": []
				},
				"actionShow": false
			}]
			newConversationList.forEach((item, index) => {
				if (!item.lastMessage.messageForShow) {
					item.lastMessage.messageForShow = '暂无新信息'
				}
				if (item.conversationID == 'C2C10087') {
					state.conversationList[0] = item
				} else if (item.conversationID == 'C2C10088') {
					state.conversationList[1] = item
				} else if (item.conversationID == 'C2C10089') {
					state.conversationList[2] = item
				}
			})
			let newConversationListCopy = []
			newConversationListCopy = newConversationList.filter((id) => {
				return id.conversationID != 'C2C10087' && id.conversationID != 'C2C10088' && id
					.conversationID != 'C2C10089'
			})
			state.conversationList = [...state.conversationList, ...newConversationListCopy]
		},
		//会话登录密码
		onChstUserSig(state, chstUserSig) {
			state.userSig = chstUserSig
		},
		/**
		 * 将消息插入当前会话列表
		 * 调用时机：收/发消息事件触发时
		 * @param {Object} state
		 * @param {Message[]|Message} data
		 * @returns
		 */
		pushCurrentMessageList(state, data) {
			// 还没当前会话，则跳过
			if (Array.isArray(data)) {
				// 筛选出当前会话的消息
				const result = data.filter(item => item.conversationID === state.conversationActive
					.conversationID)
				state.currentMessageList = [...state.currentMessageList, ...result]
			} else if (data.conversationID === state.conversationActive.conversationID) {
				state.currentMessageList = [...state.currentMessageList, data]
			}
			console.log(state.currentMessageList)
		},
		/**
		 * 滑到顶部请求更多的历史消息
		 * */
		unshiftCurrentMessageList(state, data) {
			console.log(data)
			if (data) {
				state.currentMessageList = [...data, ...state.currentMessageList]
			}
		},
	},

	actions: {

	}
})
export default store